package com.yjs.app.trade.service.service.util.search;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.github.miemiedev.mybatis.paginator.domain.Order;
import com.github.miemiedev.mybatis.paginator.domain.Order.Direction;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.yjs.app.trade.entity.search.SearchCondition;
import com.yjs.core.common.utils.SysUtils;
import com.yjs.list.model.ListParamBase;

/**
 * 查询条件工具类
 * 
 * @author Administrator
 *
 */
public class SearchConditionUtil {

	/**
	 * 查询列表参数公共方法
	 * 
	 * 编号
	 * 
	 * @param number
	 *            会员id
	 * @param memberId
	 *            会员类型
	 * @param memberType
	 *            搜索条件
	 * @param list
	 *            分页类
	 * @param pageBounds
	 * @return
	 */
	public static Map<String, Object> commonSearchCondition(String number, String memberId, int memberType,
			List<ListParamBase> list, PageBounds pageBounds) {
		Map<String, Object> maps = new HashMap<String, Object>();
		List<ListParamBase> listParamBases = new ArrayList<ListParamBase>();
		// 获取搜索条件的属性名
		List<Map<String, Object>> listMap = getFiledName(new SearchCondition());
		if (list != null && list.size() != 0) {
			for (ListParamBase listParamBase : list) {
				for (Map<String, Object> map : listMap) {
					if (listParamBase.getFieldName().equals(map.get("fieldName"))) {
						ListParamBase paramBase = new ListParamBase();
						paramBase.setFieldName(listParamBase.getFieldName());
						paramBase.setValue1(listParamBase.getValue1());
						paramBase.setFieldTypeId(map.get("fieldType").toString());
						paramBase.setEnumSearchType(map.get("enumSearchType").toString());
						listParamBases.add(paramBase);
					}
				}
			}
		}
		List<Order> orders = pageBounds.getOrders();
		Order order = Order.create("createds", Direction.DESC.toString());
		orders.add(order);
		pageBounds.setOrders(orders);
		maps.put("number", number);
		maps.put("memberId", memberId);
		maps.put("memberType", memberType);
		maps.put("list", listParamBases);
		maps.put("pageBounds", pageBounds);
		return maps;
	}

	/**
	 * 查询列表参数公共方法
	 * 
	 * 编号
	 * 
	 * @param number
	 *            会员id
	 * @param memberId
	 *            会员类型
	 * @param memberType
	 *            搜索条件
	 * @param listParamBases
	 *            分页类
	 * @param pageBounds
	 * @return
	 */
	public static Map<String, Object> searchMap(String number, String memberId, int memberType,
			List<ListParamBase> listParamBases, int pageNo, int pageSize, List<Order> orders) {
		PageBounds pageBounds = SysUtils.createPageBounds(pageNo, pageSize, true, null);
		pageBounds.setOrders(orders);
		Map<String, Object> map = new HashMap<>();
		map.put("number", number);
		map.put("memberId", memberId);
		map.put("memberType", memberType);
		map.put("list", listParamBases);
		map.put("pageBounds", pageBounds);
		return map;
	}
	
	
	/**
	 * 获取一个对象里面所有的属性
	 *
	 * @param object
	 * @return
	 */
	public static List<Map<String, Object>> getFiledName(Object object) {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		Field[] fields = object.getClass().getDeclaredFields();
		if (fields != null && fields.length != 0) {
			for (int i = 0; i < fields.length; i++) {
				Map<String, Object> map = new HashMap<String, Object>();
				// 获取属性是什么类型
				String fieldType = fields[i].getType().toString();
				// 截取
				if (fieldType.contains("class")) {
					map.put("fieldType", fieldType.substring(fieldType.lastIndexOf('.') + 1).toLowerCase());
				} else {
					map.put("fieldType", fields[i].getType().toString().toLowerCase());
				}
				map.put("fieldName", fields[i].getName());
				// 匹配条件
				if (fields[i].getName().equals("PRODUCT_CODE") || fields[i].getName().equals("ORDER_NO")
						|| fields[i].getName().equals("CREATEDS") || fields[i].getName().equals("ORDER_STATE_CODE")
						|| fields[i].getName().equals("DRUG_CODE") || fields[i].getName().equals("CONTRACT_ID")
						|| fields[i].getName().equals("STATUS_CODE")) {
					map.put("enumSearchType", "eq");
				} else {
					map.put("enumSearchType", "like");
				}
				list.add(map);
			}
		}
		return list;
	}
	
	public static ListParamBase getParamBaseValue(String fieldName, String value, String fieldType, String enumSearcheType){
		ListParamBase paramBase = new ListParamBase();
		paramBase.setIsTotal((short)1);
		paramBase.setFieldName(fieldName);
		paramBase.setValue1(value);
		paramBase.setFieldTypeId(fieldType);
		paramBase.setEnumSearchType(enumSearcheType);
		return paramBase;
	}

}
